package com.qk.state.config;

import com.qk.enums.ClueEvent;
import com.qk.enums.ClueState;
import com.qk.state.action.ClueAssingAction;
import com.qk.state.action.ClueFollowupAction;
import com.qk.state.action.ClueMarkFakeAction;
import com.qk.state.action.ClueToBusinessAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import java.util.EnumSet;

/**
 * @author 28188
 */
@Configuration
@EnableStateMachineFactory(name = "clueStateMachine")
public class ClueStateMachineConfig extends StateMachineConfigurerAdapter<ClueState, ClueEvent> {

    @Autowired
    private ClueAssingAction clueAssignAction;
    @Autowired
    private ClueFollowupAction clueFollowupAction;
    @Autowired
    private ClueToBusinessAction clueToBusinessAction;
    @Autowired
    private ClueMarkFakeAction clueMarkFakeAction;

    @Override
    public void configure(StateMachineStateConfigurer<ClueState, ClueEvent> states) throws Exception {
        states
                .withStates()
                .initial(ClueState.UNASSIGNED)
                .states(EnumSet.allOf(ClueState.class))
                .end(ClueState.CONVERTED_TO_OPPORTUNITY)
                .end(ClueState.FAKE_LEAD);
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<ClueState, ClueEvent> transitions) throws Exception {
        transitions
                // 待分配 → 待跟进
                .withExternal()
                .source(ClueState.UNASSIGNED)
                .target(ClueState.PENDING_FOLLOWUP)
                .event(ClueEvent.ASSIGN)
                .action(clueAssignAction)
                .and()

                // 待跟进 → 跟进中
                .withExternal()
                .source(ClueState.PENDING_FOLLOWUP)
                .target(ClueState.FOLLOWING_UP)
                .event(ClueEvent.START_FOLLOWUP)
                .action(clueFollowupAction)
                .and()

                // 跟进中 → 伪线索
                .withExternal()
                .source(ClueState.FOLLOWING_UP)
                .target(ClueState.FAKE_LEAD)
                .event(ClueEvent.MARK_AS_FAKE)
                .action(clueMarkFakeAction)
                .and()

                // 跟进中 → 转为商机
                .withExternal()
                .source(ClueState.FOLLOWING_UP)
                .target(ClueState.CONVERTED_TO_OPPORTUNITY)
                .event(ClueEvent.CONVERT_TO_OPPORTUNITY)
                .action(clueToBusinessAction)
                .and()

                // 跟进中 → 跟进中（自环，用于继续跟进）
                .withExternal()
                .source(ClueState.FOLLOWING_UP)
                .target(ClueState.FOLLOWING_UP)
                .event(ClueEvent.START_FOLLOWUP)
                .action(clueFollowupAction);

    }
}
